package com.gery.tn.backend.service.jdbc;

import com.gery.tn.backend.model.Appointment;
import com.gery.tn.backend.service.AppointmentService;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;



public class JDBCAppointmentService implements AppointmentService {

    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
    private PreparedStatement preparedStatement;
    
        
   
    public JDBCAppointmentService() {
        try{
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/tianails", "root", "");
            statement = connection.createStatement();
        } catch (SQLException e){
            System.out.println(e.getMessage());
        }
    }
    
    
    
    @Override
    public void saveAppointment(Appointment appointment) {
        String dateStr = toString(appointment.getDate());
        String note = appointment.getNote();
        String clientName = appointment.getClientName();
        long clientID = appointment.getClientID();
        try {
            if (appointment.getID() == 0) {
                saveWithPrepStatement(dateStr, note, clientName, clientID);
            } else {
                statement.execute("UPDATE appointments SET appointment='" + dateStr + "',note='" + note + "', clientname='" + clientName + "' WHERE id=" + appointment.getID());
            }
        } catch (SQLException ex) {
            Logger.getLogger(JDBCAppointmentService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void removeAppointment(long id) {
        try {
            statement.execute("DELETE FROM appointments WHERE id=" + id);
        } catch (SQLException ex) {
            Logger.getLogger(JDBCAppointmentService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public List<Appointment> getAllAppointments() {
        List<Appointment> temp = new ArrayList<>();
        try {
            resultSet = statement.executeQuery("SELECT id, appointment, note, clientname, clientid FROM appointments");
            while (resultSet.next()) {
                Appointment tempApp = new Appointment(this.parseDate(resultSet.getString("appointment")), resultSet.getString("note"), resultSet.getString("clientname"), resultSet.getLong("clientid"));
                tempApp.setID(resultSet.getLong("id"));
                temp.add(tempApp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(JDBCClientService.class.getName()).log(Level.SEVERE, null, ex);
        }
        return temp;
    }
    
    
    
    public void saveWithPrepStatement(String dateStr, String note, String clientName, long clientID) throws SQLException {
        preparedStatement = connection.prepareStatement("INSERT INTO appointments (appointment, note, clientname, clientid) VALUES (?, ?, ?, ?)");
        preparedStatement.setString(1, dateStr);
        preparedStatement.setString(2, note);
        preparedStatement.setString(3, clientName);
        preparedStatement.setLong(4, clientID);
        preparedStatement.executeUpdate();
    }
    public Calendar parseDate(String date) {
        Calendar tempDate = GregorianCalendar.getInstance();
        String[] dateComp = date.split("/");
        tempDate.set(Calendar.YEAR, Integer.parseInt(dateComp[0]));
        tempDate.set(Calendar.MONTH, Integer.parseInt(dateComp[1]));
        tempDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dateComp[2]));
        tempDate.set(Calendar.HOUR_OF_DAY, Integer.parseInt(dateComp[3]));
        tempDate.set(Calendar.MINUTE, Integer.parseInt(dateComp[4]));
        return tempDate;
    }
    public String toString(Calendar date) {
        String tempString = "";
        tempString = tempString + date.get(Calendar.YEAR) + "/";
        tempString = tempString + date.get(Calendar.MONTH) + "/";
        tempString = tempString + date.get(Calendar.DAY_OF_MONTH) + "/";
        tempString = tempString + date.get(Calendar.HOUR_OF_DAY) + "/";
        tempString = tempString + date.get(Calendar.MINUTE);
        return tempString;
    }
    
}
